package com.kgc.controller;

import com.kgc.basic.pojo.Account;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.feign.AccountFeign;
import com.kgc.feign.ImportExportFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/Account")
@CrossOrigin
public class AccountController {

    private Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountFeign accountFeign;

    @Autowired
    private ImportExportFeign importExportFeign;

    @RequestMapping("/selectAccountByPage")
    public ResponseMsg selectAccountByPage(@RequestBody Map<String, Object> map) {
        logger.info("AccountController   selectAccountByPage params {}", map);
        return accountFeign.selectAccountByPage(map);
    }

    @RequestMapping("/insertAccount")
    public ResponseMsg insertAccount(@RequestBody Account account) {
        logger.info("AccountController  insertAccount params {}", account);
        return accountFeign.insertAccount(account);
    }

    @RequestMapping("/updateAccount")
    public ResponseMsg updateAccount(@RequestBody Account account) {
        logger.info("AccountController  updateAccount params {}", account);
        return accountFeign.updateAccount(account);
    }

    @RequestMapping("/deleteAccount")
    public ResponseMsg deleteAccount(@RequestParam("id") String id) {
        logger.info("AccountController  deleteAccount params {}", id);
        return accountFeign.deleteAccount(Integer.parseInt(id));
    }

    @RequestMapping("selectAccountByCode")
    public ResponseMsg selectAccountByCode(@RequestParam("code") String code) {
        logger.info("consumer AccountController selectAccountByCode code:{}", code);
        return accountFeign.selectAccountByCode(code);
    }

    @RequestMapping("/selectAllAccount")
    public ResponseMsg selectAllAccount() {
        logger.info("AccountController   selectAccountByPage ");
        return accountFeign.selectAllAccount();
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseMsg importInfo(@RequestPart("file") MultipartFile file) {
        logger.info("consumer CustomerController importInfo fileName:{}", file.getOriginalFilename());
        return importExportFeign.importInfo(file, "com.kgc.basic.pojo.Account", "account", "customerServiceImpl");
    }

    @RequestMapping(value = "/template", method = RequestMethod.GET)
    public void downTemplate(HttpServletResponse response) {
        logger.info("consumer CustomerController downTemplate is start...");
        try {
            ResponseMsg responseMsg = importExportFeign.downLoadTemplate("com.kgc.basic.pojo.Account");
            if ("200".equals(responseMsg.getCode())) {
                String base64Data = (String) responseMsg.getData();
                byte[] templateData = Base64.getDecoder().decode(base64Data);   //解码

                //设置响应头
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("UTF-8");
                String filename = URLEncoder.encode("账户信息表.xlsx", "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

                response.getOutputStream().write(templateData);
            } else {
                logger.error("下载模板失败, status: {}", responseMsg.getMsg());
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            logger.error("consumer CustomerController downTemplate is error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/export")
    public void exportCustomers(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        logger.info("consumer CustomerController exportCustomers params:{}", map);
        try {
            ResponseMsg responseMsg = importExportFeign.exportEntities(map, "com.kgc.basic.pojo.Account", "");
            if ("200".equals(responseMsg.getCode())) {
                String base64Data = (String) responseMsg.getData();
                byte[] exportData = Base64.getDecoder().decode(base64Data);

                //设置响应头
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("UTF-8");
                String filename = URLEncoder.encode("账户信息表.xlsx", "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

                response.getOutputStream().write(exportData);
            } else {
                logger.error("导出失败, msg: {}", responseMsg.getMsg());
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            logger.error("consumer CustomerController exportCustomers is error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/print", method = RequestMethod.POST)
    public void printCustomer(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        logger.info("consumer CustomerController printCustomer params:{}", map);
        try {
            ResponseMsg responseMsg = importExportFeign.printEntitiesToPDF(map, "com.kgc.basic.pojo.Account", "");
            if ("200".equals(responseMsg.getCode())) {
                String base64Data = (String) responseMsg.getData();
                byte[] pdfData = Base64.getDecoder().decode(base64Data);

                response.setContentType("application/pdf");
                response.setCharacterEncoding("UTF-8");
                String filename = URLEncoder.encode("账户信息表.pdf", "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

                response.getOutputStream().write(pdfData);
            } else {
                logger.error("打印失败, msg: {}", responseMsg.getMsg());
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            logger.error("consumer CustomerController printCustomer is error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

}
